interface maskOpts {
    height: number;
    width: number;
    name: string;
    color: cc.Color;
}

export const dynamicCreateMask = (parent?: cc.Node, options?: maskOpts): void => {
    const container = parent ? parent : cc.director.getScene();

    let mWidth: number, mHeight: number, mColor: cc.Color, mName: string;

    const viewSize = cc.view.getCanvasSize();

    mWidth = options?.width || viewSize.width;

    mHeight = options?.height || viewSize.height;

    mColor = options?.color || cc.Color.BLACK;

    mName = options?.name || 'mask_pure';

    let mask = new cc.Node(mName);

    mask.addComponent(cc.BlockInputEvents);

    mask.parent = container;

    const sf = createPureColorSprite(mWidth, mHeight, mColor);

    mask.addComponent(cc.Sprite);

    mask.getComponent(cc.Sprite).spriteFrame = sf;

    mask.opacity = 150;
};

export const createPureColorSprite = (
    width: number,
    height: number,
    color?: cc.Color
): cc.SpriteFrame => {
    let texture = new cc.Texture2D();
    let spriteFrame = new cc.SpriteFrame();
    texture.initWithData(
        new Uint8Array([color.r, color.g, color.b, color.a]),
        cc.Texture2D.PixelFormat.RGB888,
        1,
        1
    );
    spriteFrame.setTexture(texture);
    spriteFrame.setRect(cc.rect(0, 0, width, height));
    return spriteFrame;
};
